package com.ftg.learn.framework.mybatis.chapter03.service;

import com.ftg.learn.framework.mybatis.chapter01.DemoMapper;
import com.ftg.learn.framework.mybatis.chapter03.mapper.TeacherMapper;
import com.ftg.learn.framework.mybatis.chapter03.util.MybatisUtil;
import com.ftg.learn.framework.mybatis.chapter03.util.R;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

/**
 * @author 布衣
 */
public class TeacherService {

    TeacherMapper teacherMapper;
    MybatisUtil m = new MybatisUtil();

    public R login(String account, String password){
        if(account == null || account.equals("") || account.length() < 1){
            return R.error("账号不能为空");
        }

        if(password == null){
            return R.error("密码不能为空");
        }

        teacherMapper = m.getSession().getMapper(TeacherMapper.class);

        int accountNo = teacherMapper.selectOneByAccount(account);
        if(accountNo == 0){
            return R.error("账号不存在");
        }

        String str = teacherMapper.selectOneByAccountAndPassword(account);
        if(str == null){
            return R.error("密码错误");
        }

        return R.ok("登录成功",str);
    }

}
